package com.archgeek.calcite;

import org.apache.calcite.sql.type.SqlTypeName;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

/**
 *
 * author: id,name,age
 * book:   id,aid,name,type
 *
 * @author pizhihui
 * @date 2024-05-30 22:57
 */
public class Storage {

    public static final String SCHEMA_NAME = "bookshop";
    public static final String TABLE_AUTHOR = "AUTHOR";
    public static final String TABLE_BOOK = "BOOK";

    public static Hashtable<String,DummyTable> _bag = new Hashtable<>();

    static {
        DummyTable author = new DummyTable(TABLE_AUTHOR);
        DummyColumn id = new DummyColumn("ID","integer");
        DummyColumn name = new DummyColumn("NAME","varchar");
        DummyColumn age = new DummyColumn("AGE","integer");
        DummyColumn aid = new DummyColumn("AID","integer");
        DummyColumn type = new DummyColumn("TYPE","varchar");

        author.addCol(id).addCol(name).addCol(age);
        author.addRow("1","jacky","33");
        author.addRow("2","wang","23");
        author.addRow("3","dd","32");
        author.addRow("4","ma","42");

        _bag.put(TABLE_AUTHOR,author);

        DummyTable book = new DummyTable(TABLE_BOOK);
        book.addCol(id).addCol(aid).addCol(name).addCol(type);
        book.addRow("1","1","数据山","java");
        book.addRow("2","2","大关","sql");
        book.addRow("3","1","lili","sql");
        book.addRow("4","3","ten","c#");

        _bag.put(TABLE_BOOK,book);
    }

    public static Collection<DummyTable> getTables(){
        return _bag.values();
    }
    public static DummyTable getTable(String tableName){return _bag.get(tableName);}


    public static class DummyTable{
        private String name;
        private List<DummyColumn> columns;
        private List<List<Object>> datas = new ArrayList<>();

        public DummyTable(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public List<DummyColumn> getColumns() {
            return columns;
        }

        public void setColumns(List<DummyColumn> columns) {
            this.columns = columns;
        }

        public DummyTable addCol(DummyColumn column) {
            if (this.columns == null) {
                columns = new ArrayList<>();
            }
            this.columns.add(column);
            return this;
        }

        public Object[] getData(int index) {
            return this.datas.get(index).toArray();
        }

        public int getRowCount() {
            return datas.size();
        }

        public void addRow(Object... objects) {
            this.datas.add(Arrays.asList(objects));
        }
    }
    public static class DummyColumn{
        private String name;
        private String type;

        public DummyColumn(String name, String type) {
            this.name = name;
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }


        public Class getJavaClass() {
            return DataTypeMapping.getJavaClassByName(this.type);
        }

        public SqlTypeName getSqlType() {
            return DataTypeMapping.getSqlTypeByName(this.type);
        }

    }
}
